package pATT.DataSetsGenerator.dataSetsGenerator.generator.visual;


import java.awt.Font;
import java.awt.HeadlessException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import pATT.DataSetsGenerator.dataSetsGenerator.attribute.implement.Attribute;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Relation;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.RelationManager;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Support;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.DataManager;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.IData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomGaussianData;
import pATT.help.visual.HelpFrame;
import pATT.logger.Message;

@SuppressWarnings("serial")
public class RandomGaussianFrame extends RandomDistributionFrame{
	
	
	private JPanel jGaussianPanel1 = null;  //  @jve:decl-index=0:visual-constraint="10,54"
	private JLabel jLabel4 = null;
	private JTextField jTextField = null;
	private JLabel jLabel5 = null;
	private JTextField jTextField1 = null;
	private JButton jButton = null;
	private JComboBox jComboBox = null;  //  @jve:decl-index=0:visual-constraint="166,12"
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField2 = null;
	
	public RandomGaussianFrame() throws HeadlessException {
		super();
		
		nameGenerator = "Random Gaussian Generator";
		Relation relation = RelationManager.instance.getCurrentRelation();
		Vector<IData> vec = new Vector<IData>();
		vec = generateData(relation.getAtributes());
		manager = new DataManager(relation,vec,nameGenerator);
		
		super.setTitle("Relation Generator Rule - "+nameGenerator);
		super.jPanel.add(getJComboBox(), null);
		super.jPanel.add(getJGaussianPanel1(), null);
		super.jLabel1.setText(manager.getRelation().getName());
		
	}
	
	private Vector<IData> generateData(Vector<Attribute> attributes) {
		Vector<IData> vector = new Vector<IData>();
		for (int i = 0; i < attributes.size(); i++) {
			vector.addElement(new RandomGaussianData(attributes.elementAt(i)));
		}
		return vector;
	}
	
	
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jComboBox.setForeground(java.awt.Color.blue);
			Vector<Attribute> vecAttributes = manager.getRelation().getAtributes();
			jComboBox.setSize(new java.awt.Dimension(95,23));
			jComboBox.setLocation(new java.awt.Point(213,10));
			for (int i = 0; i < vecAttributes.size(); i++) {
				if(vecAttributes.get(i).getType().equals("Integer") || vecAttributes.get(i).getType().equals("Real"))
					jComboBox.addItem(vecAttributes.get(i).getName());		
			}
			jComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(jComboBox.getSelectedIndex() != -1)
					if(jComboBox != null && jGaussianPanel1 != null){
						jGaussianPanel1.setVisible(true);
						jErrorLabel1.setVisible(false);
						int pos = jComboBox.getSelectedIndex();
						
						RandomGaussianData data = (RandomGaussianData)manager.getData(pos);
						int size = data.getAttribute().getValues().size();
						
						jTextField2.setEditable(false);
						jTextField2.setText(String.valueOf(size));
						if(data.getMu() != -1){
							jTextField.setText(String.valueOf(data.getMu()));
							jTextField1.setText(String.valueOf(data.getSigma()));
						}
						else{
							jTextField.setText("");
							jTextField1.setText("");
						}
						
					}
					
				}
			});
			
		}
		return jComboBox;
	}
	
	
	private JPanel getJGaussianPanel1() {
		if (jGaussianPanel1 == null) {
			/*		jErrorLabel1 = new JLabel();
			 jErrorLabel1.setBounds(new java.awt.Rectangle(0,103,330,21));
			 jErrorLabel1.setForeground(Color.red);
			 jErrorLabel1.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 11));
			 jErrorLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			 jErrorLabel1.setText("Please, complete all attribute\'s median");
			 jErrorLabel1.setVisible(false);
			 */
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(132,1,90,28));
			jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel1.setText("Attribute\'s values");
			jLabel1.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 11));
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(35,28,150,25));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setText("Gaussian Values");
			jLabel.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 14));
			jLabel5 = new JLabel();
			jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel5.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel5.setLocation(new java.awt.Point(53,77));
			jLabel5.setSize(new java.awt.Dimension(53,23));
			jLabel5.setText("Sigma");
			jLabel4 = new JLabel();
			jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel4.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel4.setSize(new java.awt.Dimension(53,23));
			jLabel4.setLocation(new java.awt.Point(53,54));
			jLabel4.setText("Mu");
			jGaussianPanel1 = new JPanel();
			jGaussianPanel1.setLayout(null);
			jGaussianPanel1.setBackground(java.awt.SystemColor.activeCaptionBorder);
			jGaussianPanel1.setEnabled(true);
			jGaussianPanel1.setVisible(false);
			jGaussianPanel1.setLocation(new java.awt.Point(7,51));
			jGaussianPanel1.setSize(new java.awt.Dimension(330,124));
			jGaussianPanel1.add(jLabel4, null);
			jGaussianPanel1.add(getJTextField(), null);
			jGaussianPanel1.add(jLabel5, null);
			jGaussianPanel1.add(getJTextField1(), null);
			jGaussianPanel1.add(getJButton(), null);
			jGaussianPanel1.add(jLabel, null);
			jGaussianPanel1.add(jLabel1, null);
			jGaussianPanel1.add(getJTextField2(), null);
			//jGaussianPanel1.add(jErrorLabel1, null);
		}
		return jGaussianPanel1;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setLocation(new java.awt.Point(105,54));
			jTextField.setSize(new java.awt.Dimension(62,23));
		}
		return jTextField;
	}
	
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setLocation(new java.awt.Point(105,77));
			jTextField1.setSize(new java.awt.Dimension(62,23));
		}
		return jTextField1;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setLocation(new java.awt.Point(171,70));
			jButton.setToolTipText("Set attribute");
			jButton.setText("Set");
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setBackground(java.awt.SystemColor.activeCaptionBorder);
			jButton.setSize(new java.awt.Dimension(62,23));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(jComboBox != null){
						int pos = jComboBox.getSelectedIndex();
						//	Attribute attr = randomGenerator.getRelation().getAtributes().get(pos);
						RandomGaussianData data = (RandomGaussianData)manager.getData(pos);
						
						if (isGaussianCorrect(data.getAttribute())){
							double mu = java.lang.Double.parseDouble(jTextField.getText());
							double sigma = java.lang.Double.parseDouble(jTextField1.getText());
							data.setMu(mu);
							data.setSigma(sigma);
							
							jGaussianPanel1.setVisible(false);
							jErrorLabel1.setVisible(false);
						}
					}
					
				}
			});
		}
		return jButton;
	}
	
	protected boolean isGaussianCorrect(Attribute attr) {
		int sizeNum = attr.getValues().size();
		String size = String.valueOf(sizeNum);
		boolean value=true;
		if(!Support.isDoublePositive(jTextField1.getText()))
		{
			jTextField1.setText("<complete>");
			value=false;
		}
		if(!Support.minusRealThan(jTextField.getText(), size)){
			jTextField.setText("<complete>");
			value=false;
		}
		return value;
	}
	
	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new java.awt.Rectangle(221,1,65,28));
			jTextField2.setHorizontalAlignment(JTextField.CENTER);
			jTextField2.setEditable(true);
		}
		return jTextField2;
	}
	@Override
	protected void callHelp() {
		new HelpFrame(Message.TITLE_RANDOM_GAUS,Message.BODY_RANDOM_GAUS).show();
		
	}
}
